{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写Python将对全体数据进行标准化，并将基础数据的特征进行重构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import numpy as np\n",
    "\n",
    "with open('data/data.pkl', 'rb') as f:\n",
    "    data = pickle.load(f)\n",
    "\n",
    "SEQLEN = 21\n",
    "dim_in = 4\n",
    "dim_out = 4\n",
    "pred_len = 30\n",
    "vmean = data.iloc[:,:4].apply(lambda x:np.mean(x))\n",
    "vstd = data.iloc[:,:4].apply(lambda x:np.std(x))\n",
    "t0 = data.iloc[:,:4].apply(lambda x:(x-np.mean(x))/np.std(x)).values\n",
    "X_train = np.zeros((t0.shape[0]-SEQLEN-pred_len, SEQLEN, dim_in))\n",
    "Y_train = np.zeros((t0.shape[0]-SEQLEN-pred_len, dim_out),)\n",
    "X_test = np.zeros((pred_len, SEQLEN, dim_in))\n",
    "Y_test = np.zeros((pred_len, dim_out),)\n",
    "for i in range(SEQLEN, t0.shape[0]-pred_len):\n",
    "    Y_train[i-SEQLEN] = t0[i]\n",
    "    X_train[i-SEQLEN] = t0[(i-SEQLEN):i]\n",
    "for i in range(t0.shape[0]-pred_len,t0.shape[0]):\n",
    "    Y_test[i-t0.shape[0]+pred_len] = t0[i]\n",
    "    X_test[i-t0.shape[0]+pred_len] = t0[(i-SEQLEN):i]\n",
    "    \n",
    "with open('data/X_train.pkl', 'wb') as f:\n",
    "    pickle.dump(X_train, f)\n",
    "\n",
    "with open('data/Y_train.pkl', 'wb') as f:\n",
    "    pickle.dump(Y_train, f)\n",
    "\n",
    "with open('data/X_test.pkl', 'wb') as f:\n",
    "    pickle.dump(X_test, f)\n",
    "\n",
    "with open('data/Y_test.pkl', 'wb') as f:\n",
    "    pickle.dump(Y_test, f)\n",
    "    \n",
    "with open('data/vstd.pkl', 'wb') as f:\n",
    "    pickle.dump(vstd, f)\n",
    "\n",
    "with open('data/vmean.pkl', 'wb') as f:\n",
    "    pickle.dump(vmean, f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
